package com.css.bjdt_core.report.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.dataSetManage.entity.DataItem;
import com.css.bjdt_core.dataSetManage.entity.DataSet;
import com.css.bjdt_core.dataSetManage.service.DataItemService;
import com.css.bjdt_core.dataSetManage.service.DataSetService;
import com.css.bjdt_core.report.entity.RptTemplateStatCategory;
import com.css.bjdt_core.report.entity.ViewRptStatCategory;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class ReportStatCategoryService {
	@Autowired
	private Dao dao;
	@Autowired
	private DataSetService dataSetService;

	@Autowired
	private DataItemService dataItemService;

	//先删除这个报表的统计信息集，再重新添加
	public RptTemplateStatCategory insert(RptTemplateStatCategory record) {
		dao.clear(RptTemplateStatCategory.class, Cnd.where("TEMPLATE_ID", "=", record.getTemplateId()));
		if (StringUtils.isBlank(record.getId())) {
			record.setId(CommonUtil.getUUID());
		}
		return dao.insert(record);
	}

	public boolean delete(String templateId) {
		if (StringUtils.isBlank(templateId)) {
			return false;
		}
		dao.clear(RptTemplateStatCategory.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		return true;
	}

	//根据报表ID获得统计信息集(这样写性能不好)
	public List<ViewRptStatCategory> getStatCategoryByTemplateId(String templateId) {
		if (StringUtils.isBlank(templateId)) {
			return null;
		}
		List<ViewRptStatCategory> statCategorys = dao.query(ViewRptStatCategory.class,
				Cnd.where("TEMPLATE_ID", "=", templateId));
		for (ViewRptStatCategory statCategory : statCategorys) {
			String setName = dao.fetch(DataSet.class, statCategory.getSetId()).getTableName();
			statCategory.setSetName(setName);
//			String itemName = dao.fetch(DataItem.class, statCategory.getItemId()).getItemName();
//			statCategory.setItemName(itemName);
			String itemUnitName = dao.fetch(DataItem.class, statCategory.getItemUnitId()).getItemName();
			statCategory.setItemUnitName(itemUnitName);
			String itemStatTimeId = statCategory.getItemStatTimeId();
			if (StringUtils.isNotBlank(itemStatTimeId)) {
				statCategory.setItemStatTimeName(dao.fetch(DataItem.class, itemStatTimeId).getItemName());
			}
		}
		return statCategorys;
	}

	//直接保存报表统计类别
	public void saveReportStatCategory(RptTemplateStatCategory record) {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(CommonUtil.getUUID());
		}
		dao.insert(record);
	}

	//分页查询
	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, RptTemplateStatCategory.class, cnd, pageNum, pageSize);
		List<RptTemplateStatCategory> rptTemplateStatCategoryList = (List<RptTemplateStatCategory>) result.get("Rows");
		for (RptTemplateStatCategory rptTemplateStatCategory : rptTemplateStatCategoryList) {
			DataSet dataSet = dataSetService.querySetById(rptTemplateStatCategory.getSetId());
			if (CommonUtil.isNullOrEmpty(dataSet)) {
				rptTemplateStatCategory.setSetName("");
			} else {
				rptTemplateStatCategory.setSetName(dataSet.getTableName());
			}
			if (rptTemplateStatCategory.getItemStatTimeId() == null) {
				rptTemplateStatCategory.setItemStatTimeId("");
			}
			DataItem dataItemTime = dataItemService.querySetById(rptTemplateStatCategory.getItemStatTimeId());
			if (CommonUtil.isNullOrEmpty(dataItemTime)) {
				rptTemplateStatCategory.setItemStatTimeName("");
			} else {
				rptTemplateStatCategory.setItemStatTimeName(dataItemTime.getItemName());
			}
			if (rptTemplateStatCategory.getItemUnitId() == null) {
				rptTemplateStatCategory.setItemUnitId("");
			}
			DataItem dataItemUnit = dataItemService.querySetById(rptTemplateStatCategory.getItemUnitId());
			if (CommonUtil.isNullOrEmpty(dataItemUnit)) {
				rptTemplateStatCategory.setItemUnitName("");
			} else {
				rptTemplateStatCategory.setItemUnitName(dataItemUnit.getItemName());
			}
		}
		result.put("Rows", rptTemplateStatCategoryList);
		return result;
	}

	//删除一个统计信息集
	public void deleteAReportStatCategoryById(String id) {
		dao.clear(RptTemplateStatCategory.class, Cnd.where("ID", "=", id));
	}

	//得到一个统计信息集
	public RptTemplateStatCategory getAReportStatCategoryById(String id) {
		return dao.fetch(RptTemplateStatCategory.class, id);
	}

	//得到所有统计信息集
	public List<RptTemplateStatCategory> getAllReportStatCategory() {
		return dao.query(RptTemplateStatCategory.class, null);
	}

	public void updateReportStatCategory(RptTemplateStatCategory record) {
		dao.updateIgnoreNull(record);
	}
}
